package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {
    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    /**
     * 新增线索跟进记录
     *
     * @param clueTrackRecord
     * @return
     */
    @Override
    public int add(ClueTrackRecordVo clueTrackRecord) {
        //String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //构建线索跟进的对象
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        //复制属性值
        BeanUtils.copyProperties(clueTrackRecord, tbClueTrackRecord);
        //设置剩余未改的属性
        tbClueTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());

        //构建线索对象
        ClueTrackRecordVo vo = new ClueTrackRecordVo();
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecord, tbClue);
        //修改状态值为进行中
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClue.setId(clueTrackRecord.getClueId());



        //修改线索数据
        int tbClueRow = tbClueMapper.updateTbClue(tbClue);
        //新增一条线索跟进记录
        int tbClueTrackRecordRow = tbClueTrackRecordMapper.add(tbClueTrackRecord);
        return (tbClueRow > 0 && tbClueTrackRecordRow > 0) ? 1 : 0;
    }

    /**
     * 查看线索跟进记录
     *
     * @param clueId
     * @return
     */
    @Override
    public List<TableDataInfo> selectTbClueTrackRecord(Long clueId) {
        return tbClueTrackRecordMapper.selectByClueId(clueId);
    }
}
